home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / OS / FWMemory / Sources / FWMemTas.cpp < prev    next >
Encoding:
Text File  |  1994-04-21  |  4.4 KB  |  148 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWMemTas.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Creation Date:        3/28/94
  7. //
  8. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include <stdlib.h>
  13.  
  14. #ifndef   FWMEMTAS_H
  15. #include "FWMemTas.h"
  16. #endif
  17.  
  18. #ifndef FWEXCDEF_H
  19. #include "FWExcDef.h"
  20. #endif
  21.  
  22. #ifndef FWPRIDEB_H
  23. #include "FWPriDeb.h"
  24. #endif
  25.  
  26. #ifndef FWOBJECH_H
  27. #include "FWObjecH.h"
  28. #endif
  29.  
  30. //----------------------------------------------------------------------------------------
  31. // Memory allocation glue code
  32. //----------------------------------------------------------------------------------------
  33.  
  34. static void* OperatorNewHandler(size_t size)
  35. {
  36. #ifdef FW_qUsePlatformAlloc
  37.     return ::malloc(size);
  38. #else
  39.     return FW_CMemoryTaskGlobals::GetMemoryGlobals().gMemoryHeap->Allocate(size);
  40. #endif
  41. }
  42.  
  43. static void OperatorDeleteHandler(void* p)
  44. {
  45. #ifdef FW_qUsePlatformAlloc
  46.     ::free(p);
  47. #else
  48.     FW_CMemoryTaskGlobals::GetMemoryGlobals().gMemoryHeap->Free(p);
  49. #endif
  50. }
  51.  
  52. //----------------------------------------------------------------------------------------
  53. // FW_CMemoryTaskGlobals::Initialize()
  54. //----------------------------------------------------------------------------------------
  55.  
  56. void FW_CMemoryTaskGlobals::Initialize(FW_SPrivMemoryGlobals& memGlobals)
  57. {
  58.     memGlobals.gMemoryHeap = 0;
  59.     InitializeMemoryHeap(memGlobals);
  60.     memGlobals.gLastRequested = 0;
  61.     memGlobals.gNewHandler = FW_CMemoryManager::DefaultNewHandler;
  62.  
  63.     FW_SPrivExceptionGlobals excGlobals = FW_CExceptionTaskGlobals::GetExceptionGlobals();
  64.  
  65. #ifndef FW_qUsePlatformAlloc
  66.     if (excGlobals.gOperatorNewHandler == 0)
  67.     {
  68.         excGlobals.gOperatorNewHandler = &::operator new;
  69.         excGlobals.gOperatorDeleteHandler = &::operator delete;
  70.     }
  71.     
  72.     // Note: Local variables are used here because it illegal to use the address of an
  73.     // overloaded function in an expression, as in the asserts below. See ARM pg. 327
  74.     // for legal uses of the address of an overloaded function.
  75.     
  76.     __FW_OperatorNewHandler newHandler = &::operator new;
  77.     __FW_OperatorDeleteHandler deleteHandler = &::operator delete;
  78.     
  79.     FW_ASSERT(excGlobals.gOperatorNewHandler == newHandler);
  80.     FW_ASSERT(excGlobals.gOperatorDeleteHandler == deleteHandler);
  81. #endif
  82. }
  83.  
  84. //----------------------------------------------------------------------------------------
  85. // FW_CMemoryTaskGlobals::Terminate()
  86. //----------------------------------------------------------------------------------------
  87.  
  88. void FW_CMemoryTaskGlobals::Terminate()
  89. {
  90.     TerminateMemoryHeap();
  91. }
  92.  
  93.  
  94. //----------------------------------------------------------------------------------------
  95. // FW_CMemoryTaskGlobals::SetMemoryHeap()
  96. //----------------------------------------------------------------------------------------
  97.  
  98. void FW_CMemoryTaskGlobals::SetMemoryHeap(FW_CMemoryHeap *aMemoryHeap)
  99. {
  100.     GetMemoryGlobals().gMemoryHeap = aMemoryHeap;
  101. }
  102.  
  103. //----------------------------------------------------------------------------------------
  104. // FW_CMemoryTaskGlobals::InitializeMemoryHeap()
  105. //----------------------------------------------------------------------------------------
  106. #ifdef FW_BUILD_MAC
  107. pascal long GetA5() = { 0x2e8d };
  108. #endif
  109.  
  110. void FW_CMemoryTaskGlobals::InitializeMemoryHeap(FW_SPrivMemoryGlobals& memGlobals)
  111. {
  112.     const unsigned long kDefaultHeapSize = 10L * 1024L;
  113.     const unsigned long kDefaultHeapIncrementSize = 25L * 1024L;
  114.     
  115. #ifdef FW_qUsePlatformAlloc
  116.     memGlobals.gMemoryHeap = NULL;
  117.     memGlobals.gInitialized = 1;
  118. #else
  119.     FW_CBestFitHeap *heap = new FW_CBestFitHeap(kDefaultHeapSize,
  120.                                                 kDefaultHeapIncrementSize);
  121.     FW_ASSERT(heap != NULL);
  122.  
  123.     memGlobals.gMemoryHeap = heap;
  124. #ifdef FW_DEBUG
  125.     heap->SetDescription("Default OPF Heap");
  126.     heap->SetZapOnAllocate(TRUE);
  127.     heap->SetZapOnFree(TRUE);
  128.     heap->SetAutoValidation(TRUE);
  129. #else
  130.     heap->SetZapOnAllocate(FALSE);
  131.     heap->SetZapOnFree(FALSE);
  132.     heap->SetAutoValidation(FALSE);
  133. #endif
  134. #endif
  135. }
  136.  
  137. //----------------------------------------------------------------------------------------
  138. // FW_CMemoryTaskGlobals::TerminateMemoryHeap()
  139. //----------------------------------------------------------------------------------------
  140. void FW_CMemoryTaskGlobals::TerminateMemoryHeap()
  141. {
  142. #ifndef FW_qUsePlatformAlloc
  143.     FW_SPrivMemoryGlobals& memGlobals = GetMemoryGlobals();
  144.     delete memGlobals.gMemoryHeap;
  145.     memGlobals.gMemoryHeap = NULL;
  146. #endif
  147. }
  148.